package com.san.metadata.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.druid.pool.DruidPooledPreparedStatement;
import com.san.base.dao.BaseManager;
import com.san.common.datasource.JdbcManager;
import com.san.common.utils.MD5Utils;
import com.san.common.utils.StringUtils;
import com.san.metadata.vo.BaseLegalPersonVO;
import com.san.metadata.vo.BaseNaturalPersonVO;

@Service
public class BasePersonCache extends BaseManager{
	private Map<String,BaseLegalPersonVO> legalPersonNameCache;
	private Map<String,BaseLegalPersonVO> usccCache;
	private Map<String,String> naturalCache;
	
	/**
	 * 初始法人base表缓存
	 */
	public void initLegalCache() {
		if (null == legalPersonNameCache || null == usccCache) {
			legalPersonNameCache = new HashMap<String,BaseLegalPersonVO>();
			usccCache = new HashMap<String,BaseLegalPersonVO>();
			DruidPooledConnection conn = null;
			DruidPooledPreparedStatement ps = null;
			ResultSet rs = null;
			try {
				conn = this.getBaseDBConnection();
				JdbcManager jdbcManager = new JdbcManager();
				Map<String, Object> result = jdbcManager.executeQueryByType(conn, "select legal_person_name,uscc,legal_person_id from base_legal_person", null);
				
				rs = (ResultSet) result.get("rs");
				ps = (DruidPooledPreparedStatement) result.get("ps");
				BaseLegalPersonVO vo = null;
				while (rs.next()) {
					vo = new BaseLegalPersonVO();
					vo.setLegalPersonId(rs.getString(3));
					vo.setLegalPersonName(rs.getString(1));
					vo.setUscc(rs.getString(2));
					if (!StringUtils.isEmpty(vo.getLegalPersonName())) {
						legalPersonNameCache.put(vo.getLegalPersonName(), vo);
					}
					if (!StringUtils.isEmpty(vo.getUscc())) {
						usccCache.put(vo.getUscc(), vo);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {

				try {
					if (null != ps) {
						ps.close();
					}
					if (null != conn) {
						conn.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}

			}
		}	
	}
	
	/**
	 * 初始自然人base表缓存
	 */
	public void initNaturalCache() {
		if (null == naturalCache) {
			naturalCache = new HashMap<String,String>();
			DruidPooledConnection conn = null;
			DruidPooledPreparedStatement ps = null;
			ResultSet rs = null;
			try {
				conn = this.getBaseDBConnection();
				JdbcManager jdbcManager = new JdbcManager();
				Map<String, Object> result = jdbcManager.executeQueryByType(conn, "select name,id_card,natural_person_id from base_natural_person", null);
				
				rs = (ResultSet) result.get("rs");
				ps = (DruidPooledPreparedStatement) result.get("ps");
				while (rs.next()) {
					naturalCache.put(rs.getString(1) + rs.getString(2), rs.getString(3));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {

				try {
					if (null != ps) {
						ps.close();
					}
					if (null != conn) {
						conn.close();
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}

			}
		}	
	}
	
	/**
	 * 获取LegalPersonId
	 * @param uscc
	 * @return
	 */
	public BaseLegalPersonVO getLegalPersonIdByUscc(String uscc) {
		if (StringUtils.isEmpty(uscc)) {
			return null;
		}
		
		return usccCache.get(uscc);
	}
	
	/**
	 * 获取LegalPersonId
	 * @param legalPersonName
	 * @return
	 */
	public BaseLegalPersonVO getLegalPersonIdByLegalPersonName(String legalPersonName) {
		if (StringUtils.isEmpty(legalPersonName)) {
			return null;
		}
		return legalPersonNameCache.get(legalPersonName);
	}
	
	public BaseLegalPersonVO putLegalPerson(String uscc,String legalPersonName) {
		if (StringUtils.isEmpty(legalPersonName)) {
			return null;
		}
		String legalPersonId = MD5Utils.MD5Encode(legalPersonName);
		BaseLegalPersonVO vo = new BaseLegalPersonVO();
		vo.setUscc(uscc);
		vo.setLegalPersonName(legalPersonName);
		vo.setLegalPersonId(legalPersonId);
		if (!StringUtils.isEmpty(uscc)) {
			usccCache.put(uscc, vo);
		}
		legalPersonNameCache.put(legalPersonName, vo);
		
		return vo;
	}
	
	/**
	 * 获取naturalPersonId
	 * @param name
	 * @param idCard
	 * @return
	 */
	public String getNaturalPersonId(String name, String idCard) {
		if (StringUtils.isEmpty(name) || StringUtils.isEmpty(idCard)) {
			return null;
		}
		return naturalCache.get(name+idCard);
	}
	
	public BaseNaturalPersonVO putNaturalPerson(String name, String idCard) {
		if (StringUtils.isEmpty(name) || StringUtils.isEmpty(idCard)) {
			return null;
		}
		String naturalPersonId = MD5Utils.MD5Encode(idCard);
		BaseNaturalPersonVO vo = new BaseNaturalPersonVO();
		vo.setName(name);
		vo.setIdCard(idCard);
		vo.setNaturalPersonId(naturalPersonId);
		naturalCache.put(name+idCard, naturalPersonId);
		
		return vo;
	}
	
	/**
	 * 获取全量法人
	 * @return
	 */
	public Map<String,BaseLegalPersonVO> getLegalPerson(){
		return legalPersonNameCache;
	}
}
